var express = require('express');
var router = express.Router();
var data = require('../data.js')

const eps = 1e-6;

// 数据静态地存在一个js里

// 登录
router.post('/user/login/',function(req, res, next){
    try {
        // formdata形式的形式只能用formidable提供的req.fields取数据
        let username = req.body.username;
        let password = req.body.password;
        if(data.login(username, password)){
            res.send({
                code: 200,
                msg:'登陆成功',
            });
        }
        else{
            res.send({
                code: 400,
                msg:'登陆失败',
            });
        }
    } catch (error) {
        console.log(error)
        res.send({
            code: 400,
            msg:'登陆失败',
        });
    };
});

router.post('/admin/login/',function(req, res, next){
    try {
        let username = req.body.username;
        let password = req.body.password;
        if(data.adminLogin(username, password)){
            res.send({
                code: 200,
                msg:'登陆成功',
            });
        }
        else{
            res.send({
                code: 400,
                msg:'登陆失败',
            });
        }
    } catch (error) {
        res.send({
            code: 400,
            msg:'登陆失败',
        });
    };
});

router.get('/admin/prize/pool/', function(req, res, next) {
    if (data.adminHasLogin()){
        let allPrizePool = data.getAllPrizePool();
        res.send({
            code:200,
            data:allPrizePool,
            msg: "数据获取成功"
            }
        );
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
});


// 管理员

router.post('/prize/pool/all/', function(req, res, next) {
    try {
        if (data.adminHasLogin()){
            let newPool = eval(req.body.data);
            if (newPool == null)
            {
                res.send({
                    code:400,
                    msg : '未接受到数据'
                })
                return
            }
            else{
                // 概率求和
                let probabilitySum = newPool.reduce(  (accumulator,cur) => {
                    if(cur.checked === true){
                        return accumulator + cur.probability;
                    }
                    else{
                        return accumulator;
                    }}  , 0 );
                if (probabilitySum >= 1-eps && probabilitySum <= 1+eps ){

                    data.setAllPrizePool(newPool);
                    res.send({
                        code:200,
                        msg : "数据更新成功"
                    })
                    return;
                }
                else{
                    res.send({
                        code:400,
                        msg : "奖池概率不符合要求"
                    })
                }
                
            }
        }  
        else {
            res.send({
                code :401,
                msg: '未登录'
            });
        }    
    } catch (error) {
        console.log(error);
        res.send({
            code:400,
            msg : "请求未被执行"
        })
    }

});

// 以下是用户url
router.get('/prize/pool/', function(req, res, next) {
    if (data.userHasLogin()){
        let prizePool=data.getPrizePool();
        res.send({
            code:200,
            data:prizePool,
            msg: "数据获取成功"
            }
        );
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
});
router.get('/can-signin/', function(req, res, next) {
    if(data.userHasLogin()){
        let canSignin = data.canSignin();
        if (canSignin) {
            res.send({
                code: 200,
            });
        } else {
        res.send({
            code: 201,
        });
        }
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
  });

router.get('/signin/', function(req, res, next) {
    if(data.userHasLogin()){
        let ret = data.signin();
        if(ret) {
            res.send({
                code:200,
                msg:'签到成功'
            });
        }
        else {
            res.send({
                code:201,
                msg:'已经签过到了'
            });
        }
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
});
    
router.get('/lottery/', function(req, res, next) {
    if (data.userHasLogin()){
        let index = data.draw();
        // 如果是-1,代表石头不够，如果不是那就返回奖品的index（0到7）
        if(index === -1){
            res.send({
                code:400,
                msg:'矿石数不足，无法抽奖',
            });
            return
        }
        else
        {
            let prize = data.getPrizeByIndex(index);
            prize.index = index;
            let copy =JSON.parse(JSON.stringify(prize));
            delete copy.index;
            delete copy.probability;
            data.addToPrizeList(copy);
            res.send({
                code:200,
                msg:'抽奖完成',
                prize: prize
            });
        }
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
});

router.get('/stonecoin/', function(req, res, next) {
    if( data.userHasLogin()){
        res.send({
            code: 200,
            data: {
            stonecoin: data.getStoneNum()
            },
            msg: "数据获取成功"
        });
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
});
router.get('/prize/list/', function(req, res, next) {
    if(data.userHasLogin()){
        let prizeList = data.getPrizeList();
        res.send({
            'code': 200,
            'msg': "数据获取成功",
            'data': prizeList,
        });
    }
    else {
        res.send({
            code :401,
            msg: '未登录'
        });
    }
});
router.get('/', function(req, res, next) {
    res.send('this is V1');
});

module.exports = router;